package factory;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class DisposeOnEscapeFactory {
    private static final KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
    private static final String ESCAPE_STRING = "ESCAPE";

    private DisposeOnEscapeFactory() {}

    public static void appendDisposeOnEscapeToJFrame(final JFrame jframe) {
        jframe.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, ESCAPE_STRING);
        jframe.getRootPane().getActionMap().put(ESCAPE_STRING, new AbstractAction() {
            private static final long serialVersionUID = -5447349524535859495L;

            public void actionPerformed(ActionEvent e) {
                jframe.dispose();
            }
        });
    }
}
